package com.wzt.lianfirecontrol.fragment.xiaokongshi;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.activity.ShowFragActivity;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XksRecordProblemModel;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.ScreenUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

public class UnusualDetailFragment extends BaseFragment {
    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    private TextView tv_yichang_name;
    private TextView tv_error_time;
    private TextView tv_error_content;

    private LinearLayout ll_device_three_photo;
    private View include_play_photo_three;
    private RelativeLayout rl_device_photo_1;
    private ImageView iv_device_photo_1;
    private ImageView iv_photo_delete_1;
    private RelativeLayout rl_device_photo_2;
    private ImageView iv_device_photo_2;
    private ImageView iv_photo_delete_2;
    private RelativeLayout rl_device_photo_3;
    private ImageView iv_device_photo_3;
    private ImageView iv_photo_delete_3;

    private View include_no_data;
    private View include_no_net;
    private View include_progress_bar;

    private XksRecordProblemModel xksRecordProblemModel;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            bundle = getArguments();
            activity = getMyActivity();
            if (null != bundle){
                xksRecordProblemModel = (XksRecordProblemModel) bundle.getSerializable("data");
            }
            if (null == xksRecordProblemModel){
                activity.finish();
            }
            itemContentView = inflater.inflate(R.layout.f_unusual_detail, container, false);
            initView();
            setDetailData();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        tv_yichang_name = itemContentView.findViewById(R.id.tv_yichang_name);
        tv_error_time = itemContentView.findViewById(R.id.tv_error_time);
        tv_error_content = itemContentView.findViewById(R.id.tv_error_content);

        initPhotoThree();

        include_no_data = itemContentView.findViewById(R.id.include_no_data);
        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        InitView.instance().initProgressBar(activity, include_progress_bar);
        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });
    }

    private void initPhotoThree() {
        ll_device_three_photo = itemContentView.findViewById(R.id.ll_device_three_photo);
        include_play_photo_three = itemContentView.findViewById(R.id.include_play_photo_three);
        rl_device_photo_1 = itemContentView.findViewById(R.id.rl_device_photo_1);
        iv_device_photo_1 = itemContentView.findViewById(R.id.iv_device_photo_1);
        iv_photo_delete_1 = itemContentView.findViewById(R.id.iv_photo_delete_1);
        iv_photo_delete_1.setVisibility(View.GONE);
        rl_device_photo_2 = itemContentView.findViewById(R.id.rl_device_photo_2);
        iv_device_photo_2 = itemContentView.findViewById(R.id.iv_device_photo_2);
        iv_photo_delete_2 = itemContentView.findViewById(R.id.iv_photo_delete_2);
        iv_photo_delete_2.setVisibility(View.GONE);
        rl_device_photo_3 = itemContentView.findViewById(R.id.rl_device_photo_3);
        iv_device_photo_3 = itemContentView.findViewById(R.id.iv_device_photo_3);
        iv_photo_delete_3 = itemContentView.findViewById(R.id.iv_photo_delete_3);
        iv_photo_delete_3.setVisibility(View.GONE);

        int width = (ScreenUtils.getScreenWidth(activity) - Utils.dip2px(activity, 90)) / 3;
        rl_device_photo_1.getLayoutParams().width = width;
        rl_device_photo_1.getLayoutParams().height = width;
        rl_device_photo_2.getLayoutParams().width = width;
        rl_device_photo_2.getLayoutParams().height = width;
        rl_device_photo_3.getLayoutParams().width = width;
        rl_device_photo_3.getLayoutParams().height = width;

    }

    private void setDetailData() {
        if (null == xksRecordProblemModel){
            include_no_data.setVisibility(View.VISIBLE);
            return;
        }
        include_no_data.setVisibility(View.GONE);
        tv_yichang_name.setText(xksRecordProblemModel.getName());
        tv_error_time.setText(xksRecordProblemModel.getTimeStr());
        tv_error_content.setText(xksRecordProblemModel.getRemark());
        String[] imgUrls = xksRecordProblemModel.getImgUrls().split(",");
        if (imgUrls.length > 0 && imgUrls[0].startsWith("http")){
            GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrls[0]);
            iv_device_photo_1.setVisibility(View.VISIBLE);
            iv_device_photo_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((ShowFragActivity)activity).setShowImg(imgUrls[0]);
                }
            });
        }else {
            iv_device_photo_1.setVisibility(View.GONE);
        }

        if (imgUrls.length > 1){
            GildeUtils.loadImageCorner(activity, iv_device_photo_2, imgUrls[1]);
            iv_device_photo_2.setVisibility(View.VISIBLE);
            iv_device_photo_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((ShowFragActivity)activity).setShowImg(imgUrls[1]);
                }
            });
        }else {
            iv_device_photo_2.setVisibility(View.GONE);
        }

        if (imgUrls.length > 2){
            GildeUtils.loadImageCorner(activity, iv_device_photo_3, imgUrls[2]);
            iv_device_photo_3.setVisibility(View.VISIBLE);
            iv_device_photo_3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((ShowFragActivity)activity).setShowImg(imgUrls[2]);
                }
            });
        }else {
            iv_device_photo_3.setVisibility(View.GONE);
        }
    }

    @Override
    public void updateData() {
        super.updateData();
    }
}
